package infostroy.bookagolf.components.controls;

import infostroy.bookagolf.common.Constants.Colors;
import infostroy.bookagolf.common.Constants.Icons;
import infostroy.bookagolf.common.helpers.ImageHelper;

import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

/**
 * Created by valer_000 on 2/1/14.
 */
public class GolfScrollBar extends JScrollBar {

    public GolfScrollBar() {
        this.setUI(new GolfScrollBarUI());
    }

    public GolfScrollBar(int orientation) {
        super(orientation);
        this.setUI(new GolfScrollBarUI());
    }

    private class GolfScrollBarUI extends BasicScrollBarUI implements Colors{

        private BufferedImage verticalThumbImage;
        private BufferedImage horizontalThumbImage;


        private GolfScrollBarUI() {
            verticalThumbImage = ImageHelper.getInstance().loadImage(Icons.VERTICAL_THUMB);
            horizontalThumbImage = ImageHelper.getInstance().loadImage(Icons.HORIZONTAL_THUMB);
        }

        @Override
        public Dimension minimumLayoutSize(Container scrollbarContainer) {
            return super.minimumLayoutSize(scrollbarContainer);
        }

        @Override
        protected JButton createDecreaseButton(int orientation) {
            return createZeroButton();
        }

        @Override
        protected JButton createIncreaseButton(int orientation) {
            return createZeroButton();
        }

        @Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
            g.setColor(SCROLL_TRACK_BACKGROUND);
            g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
            g.setColor(SCROLL_TRACK_BORDER);
            g.drawRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
        }

        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
            if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
                return;
            }
            g.translate(thumbBounds.x, thumbBounds.y);
            g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1);
            if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
                AffineTransform transform = AffineTransform.getScaleInstance((double) thumbBounds.width
                        / verticalThumbImage.getWidth(null), (double) thumbBounds.height / verticalThumbImage.getHeight(null));
                ((Graphics2D) g).drawImage(verticalThumbImage, transform, null);
            } else {
                AffineTransform transform = AffineTransform.getScaleInstance((double) thumbBounds.height / horizontalThumbImage.getHeight(null),
                        (double) thumbBounds.width / horizontalThumbImage.getWidth(null));
                ((Graphics2D) g).drawImage(horizontalThumbImage, transform, null);
            }
            g.translate(-thumbBounds.x, -thumbBounds.y);
        }

        @Override
        public Dimension getPreferredSize(JComponent c) {
            return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
                    ? new Dimension(verticalThumbImage.getWidth(), verticalThumbImage.getHeight())
                    : new Dimension(horizontalThumbImage.getWidth(), horizontalThumbImage.getHeight());

        }

        @Override
        protected Dimension getMinimumThumbSize() {
            return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
                    ? new Dimension(verticalThumbImage.getWidth(), verticalThumbImage.getHeight())
                    : new Dimension(horizontalThumbImage.getWidth(), horizontalThumbImage.getHeight());
        }

        @Override
        protected Dimension getMaximumThumbSize() {
            return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
                    ? new Dimension(verticalThumbImage.getWidth(), verticalThumbImage.getHeight())
                    : new Dimension(horizontalThumbImage.getWidth(), horizontalThumbImage.getHeight());
        }

        /**
         * Create empty button for removing scrollbar buttons
         *
         * @return JButton - empty button;
         */
        private JButton createZeroButton() {
            JButton button = new JButton();
            Dimension zeroDimension = new Dimension(0, 0);
            button.setPreferredSize(zeroDimension);
            button.setMinimumSize(zeroDimension);
            button.setMaximumSize(zeroDimension);
            return button;
        }
    }
}
